package com.atguigu;

import java.util.concurrent.TimeUnit;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/8/7
 */

class Phone {
    /**
     * 1. 普通同步方法
     */
    public static synchronized void sendEmail() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----Email");

    }

    public synchronized void sendSMS() {
        System.out.println("-----SMS");
    }

    public void hello() {
        System.out.println("-----hello");
    }

    Object o = new Object();

    /**
     * 2. 同步代码块
     */
    public void m2() {
        synchronized (o) {

        }
    }

    /**
     * 3. 静态同步方法
     */
    public static synchronized void m3() {

    }
}

public class Lock8Demo {
    /**
     * 1. 普通同步方法
     */
    public synchronized void m1() {

    }

    Object o = new Object();

    /**
     * 2. 同步代码块
     */
    public void m2() {
        synchronized (o) {

        }
    }

    /**
     * 3. 静态同步方法
     */
    public static synchronized void m3() {

    }


    public static void main(String[] args) {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();
        new Thread(() -> {
            phone1.sendEmail();
        }, "t1").start();

        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            phone2.sendSMS();
//            phone1.hello();
        }, "t2").start();
    }
}
